package com.pajx.facelibrary.view;

import android.content.Context;
import android.graphics.Outline;
import android.graphics.Rect;
import android.graphics.SurfaceTexture;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewOutlineProvider;

import com.pajx.facelibrary.gl.EGLUtils;
import com.pajx.facelibrary.gl.GLFaeShap;
import com.pajx.facelibrary.gl.GLFrame;
import com.pajx.facelibrary.gl.GLFramebuffer;

public class FaceSurfaceView extends SurfaceView {
    private GLFramebuffer mFramebuffer;
    private EGLUtils mEglUtils;
    private GLFrame mFrame;
    private GLFaeShap glFaeShap;

    // 圆角半径
    private final int radius = 0;

    public FaceSurfaceView(Context context) {
        super(context);
    }

    public FaceSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setOutlineProvider(new ViewOutlineProvider() {
            @Override
            public void getOutline(View view, Outline outline) {
                Rect rect = new Rect(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
                outline.setRoundRect(rect, radius);
            }
        });
        setClipToOutline(true);
    }

    public void init(int mirrorRGB) {
        mFrame = new GLFrame(mirrorRGB);
        glFaeShap = new GLFaeShap();
        mFramebuffer = new GLFramebuffer();
        if (getHolder().getSurface() != null && getWidth() > 0) {
            if (mEglUtils != null) {
                mEglUtils.release();
            }
            mEglUtils = new EGLUtils();
            mEglUtils.initEGL(getHolder().getSurface());
            mFrame.initFrame();
            mFramebuffer.initFramebuffer();

        }

    }

    public void surfaceChanged(SurfaceHolder surfaceHolder) {
        Log.d("=============", "surfaceChanged");
        if (mEglUtils != null) {
            mEglUtils.release();
        }
        mEglUtils = new EGLUtils();
        mEglUtils.initEGL(surfaceHolder.getSurface());
        mFramebuffer.initFramebuffer();

    }

    public SurfaceTexture getFramebuffer() {
        return mFramebuffer.getSurfaceTexture();
    }

    public void initFrame(int width, int height) {
//        mFramebuffer.initFramebuffer();
        mFrame.initFrame();
        glFaeShap.init(width, height);
    }

    public void surfaceDestroyed() {

        mFramebuffer.release();
        mFrame.release();

        if (mEglUtils != null) {
            mEglUtils.release();
            mEglUtils = null;
        }
    }

    private int i = -1;

    public void setFrame() {
        this.i = mFramebuffer.drawFrameBuffer();
        this.floats = mFramebuffer.getMatrix();
    }

    public void setSize(int width, int height) {
        mFrame.setSize(getWidth(), getHeight(), width, height);
        mFrame.correctSize(width, height);
    }

    private float[] floats;

    public void onGlDraw() {
        if (mEglUtils == null) {
            return;
        }
        mFrame.setS(100 / 100.0f);
        mFrame.setH(0 / 360.0f);
        mFrame.setL(100 / 100.0f - 1);
        mFrame.drawFrame(0, mFramebuffer.drawFrameBuffer(), mFramebuffer.getMatrix());
        mEglUtils.swap();
    }
}
